package com.google.code.ojar.sendtemplatemail.app.resendhandler.imp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.ojar.sendtemplatemail.app.exception.AppException;
import com.google.code.ojar.sendtemplatemail.app.library.SendMailLibrary;
import com.google.code.ojar.sendtemplatemail.app.resendhandler.ResendFactory;
import com.google.code.ojar.sendtemplatemail.app.resendhandler.ResendHandler;
import com.google.code.ojar.sendtemplatemail.app.utils.SendMailUtils;

/**
 * Concrete factory Class that instantiate a Handler
 */
public class ResendDefaultFactory extends ResendFactory {

	// logger
	private static final Logger logger = LoggerFactory
			.getLogger(ResendDefaultFactory.class);


	@Override
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.code.ojar.sendtemplatemail.app.resendhandler.ResendFactory
	 * #gethandler()
	 */
	public ResendHandler gethandler() throws Exception {
		String className = null;
		try {
			className = SendMailUtils.getPropertiesString(
					SendMailLibrary.PROPERTY_FILE_NAME,
					SendMailLibrary.PROPERTY_KEY_RESEND_MAIL_HANDLER);
		} catch (AppException e) {
			// si el nombre de la clase es null cargo el handler por default
			className = XmlHandler.class.getName();
			logger.warn("Resend Hanndler class not defined, using default instead ("
					+ className + "), because of:" + e.getMessage());
		}
		Class<?> handler = SendMailUtils.getClass(className, this.getClass()
				.getClassLoader());

		ResendHandler instance = null;
		try {
			instance = (ResendHandler) handler.newInstance();
		} catch (ClassCastException e) {
			logger.error("Can't instanciate ResendHandler(" + className
					+ ") because the class given is not a ResendHandler", e);
			throw e;
		} catch (Exception e) {
			logger.error("Can't instanciate ResendHandler(" + className
					+ ") due an error ", e);
			throw e;
		}

		return instance;
	}

	@Override
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.code.ojar.sendtemplatemail.app.resendhandler.ResendFactory
	 * #getInstance(java.lang.String)
	 */
	public ResendFactory getInstance(String factoryClassName) {
		
		throw new RuntimeException("Method not Implemented");
	}

}
